package com.neuedu.his.dao;


import com.neuedu.his.common.utils.DBUtils;
import com.neuedu.his.entity.UmsUser;

import java.util.List;
import java.util.Map;

public class UmsUserDao {

    /**
     * 查询集合
     */
    public List<UmsUser> selectList(String where , Object ... params){
        String sql =" select * from ums_user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<UmsUser> umsUsers = DBUtils.selectList(UmsUser.class, sql, params);

        return umsUsers;
    }

    /**
     * 分页查询
     * @param where
     * @param pageNo
     * @param size
     * @param params
     * @return
     */
    public List<UmsUser> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from ums_user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<UmsUser> umsUsers = DBUtils.selectList(UmsUser.class, sql, params);

        return umsUsers;
    }


    /**
     * 查询单条结果
     */
    public UmsUser selectOne(String where ,Object ... params){
        String sql =" select * from ums_user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(UmsUser.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from ums_user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
     * 根据数据进行插入
     * @param umsUser
     * @return
     */
    public int insert(UmsUser umsUser){
        String sql = "insert into ums_user(       dept_id   ,       dept_name   ,       expire   ,       lastlogin   ,       nickname   ,       password   ,       regist_level   ,       username   ) values(   ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?    )";
        return DBUtils.update( sql,    umsUser.getDeptId()    ,     umsUser.getDeptName()    ,     umsUser.getExpire()    ,     umsUser.getLastlogin()    ,     umsUser.getNickname()    ,     umsUser.getPassword()    ,     umsUser.getRegistLevel()    ,     umsUser.getUsername() );
    }


    /**
     * 根据主键更新一条
     * @param umsUser
     * @return
     */
    public int updateById(UmsUser umsUser){
        String sql = " update ums_user set     del = ?   ,       dept_id = ?   ,       dept_name = ?   ,       expire = ?   ,       lastlogin = ?   ,       nickname = ?   ,       password = ?   ,       regist_level = ?   ,       username = ?   where  userid = ?  ";

        return DBUtils.update( sql,
                umsUser.getDel()  ,
                umsUser.getDeptId()  ,
                umsUser.getDeptName()  ,
                umsUser.getExpire()  ,
                umsUser.getLastlogin()  ,
                umsUser.getNickname()  ,
                umsUser.getPassword()  ,
                umsUser.getRegistLevel()  ,
                umsUser.getUsername()  ,
                umsUser.getUserid() );
    }



    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from ums_user  where userid = ?";
        return DBUtils.update( sql, id);

    }


}